package com.du.stutestsys.controller.Administrator;

import com.alibaba.fastjson.JSONObject;
import com.du.stutestsys.controller.BaseControler;
import com.du.stutestsys.entity.SysUser;
import com.du.stutestsys.service.Administrator.SysUserService;
import com.du.stutestsys.util.PageUtil;
import com.du.stutestsys.util.DuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//管理员管理
@Controller
public class SysUserController extends BaseControler {
    @Autowired
    SysUserService sysUserService;


    @PostMapping("admin/addAdmin/{SysUser}")
    public String addAdmin(@PathVariable(required = false) SysUser sysUser){
        return DuUtil.success(sysUserService.addAmin(sysUser)).toJSONString();

    }

    @PostMapping("admin/deleteAdmin/{sysUserList}")
    public String deleteAdmin(@PathVariable(required = false) List<SysUser> sysUserList){

        return DuUtil.success(sysUserService.deleteAdmin(sysUserList)).toJSONString();
    }


    @PostMapping("admin/updateAdmin/{SysUser}")
    public String updateAdmin(@PathVariable(required = false) SysUser sysUser){
        return DuUtil.success(sysUserService.updateAdmin(sysUser)).toJSONString();
    }

    @GetMapping("admin/selectAdmin")
    public String selectUser(@RequestBody SysUser sysUser){
        //如果对象为空则查询全部
        PageUtil pageUtil = DuUtil.setPageUtil(sysUser, 0, 10);
        List<SysUser> listUser = sysUserService.selectAdmin(sysUser,pageUtil);
        JSONObject success = DuUtil.success(listUser.size() > 0, listUser, listUser+"",pageUtil);
        return success.toJSONString();
    }

    //修改管理员权限
    @PostMapping("admin/updatePrivilegeAdmin")
    public String updatePrivilegeAdmin(@RequestParam List<SysUser> sysUserList){
        return DuUtil.success(sysUserService.updatePrivilegeAdmin(sysUserList)).toJSONString();
    }

    //新增学生并注册用户
    @PostMapping("admin/addStuUser")
    public String addStuUser(@RequestBody SysUser stuUser){
        return DuUtil.success(sysUserService.addStuUser(stuUser)).toJSONString();
    }

    //批量删除学生
    @PostMapping("admin/deleteStu")
    public String deleteStu(@RequestParam List<Long> stuids){
        return DuUtil.success(sysUserService.deleteStu(stuids)).toJSONString();
    }


    //批量删除学生
    @PostMapping("admin/updateTeaSub")
    public String updateTeaSub(@RequestBody SysUser stuUser){
        return DuUtil.success(sysUserService.updateTeaSub(stuUser)).toJSONString();
    }
}
